%%% Add paths
addpath('../code/general_funcs');

platformDataFolder = '../data/data_csv/platformData';
outputFolder = '../results/EDA/summaryTimeLags';
tt_bounds = [0 476];

% Make table of timestamps for first-time activity
[M, geoLabels] = loadDataUser1stTimeActivities(platformDataFolder);
K = length(geoLabels);

% Define time bounds
disp(sprintf('Time bounds: from %d to %d', tt_bounds(1), tt_bounds(2)));

% Get timestamps
accountCreationDate  = table2array(M(:,'AccountCreationDate_julian'));
consumer1stTransacDate = table2array(M(:,'Consumer1stTransacDate_julian'));
provider1stTransacDate  = table2array(M(:,'Provider1stTransacDate_julian'));
providerCreationDate    = table2array(M(:,'providerCreationDate_julian'));
providerDeletionDate    = table2array(M(:,'providerDeletionDate_julian'));
userType             = table2array(M(:,'userType'));


%%%%%%%%%%% Duration between account creation and 1st car posted %%%%%%%%%%
%%%%%%%%%%% (for providers who became providers within tt_bounds and created user acct after tt=0) %%%%%%%%%%%
idxes = ~isnan(providerCreationDate) & providerCreationDate > tt_bounds(1) & providerCreationDate < tt_bounds(2) & accountCreationDate > tt_bounds(1);
delays = providerCreationDate(idxes) - accountCreationDate(idxes);
length(delays)

% Remove cases where the user account creation is registered more than one day after + set delay=0 if delay < 0
delays = delays(delays > -1);
delays = max(0,delays);

% Look at distribution
summary_func(delays)
mean(delays < 1)
mean(delays < 7)

% Percentage of providers who post a car less than one hour after creating their accounts:
disp('Percentage of providers who post a car less than one hour after creating their accounts:');
mean(delays <1/24)

makeHistogram_and_percentilePlot(delays, outputFolder, 'timeLag_carCreation', 'Time lag (days)', 45, 450);

clear delays idxes;


%%%%%%%%%%% Duration between account creation and 1st provider transaction (for providers who created their user account and had their 1st transac in the window) %%%%%%%%%%%
idxes = ~isnan(provider1stTransacDate) & provider1stTransacDate > providerCreationDate & userType == 3 & ...
		accountCreationDate > tt_bounds(1) & accountCreationDate < tt_bounds(2) & ...
	provider1stTransacDate > tt_bounds(1) & provider1stTransacDate < tt_bounds(2);

delays = provider1stTransacDate(idxes) - accountCreationDate(idxes);

% Look at distribution
disp('===== Delay between provider account creation and 1st transaction: =====');
summary_func(delays)

disp('Median delay between account creation and first transaction for providers:');
median(delays)

makeHistogram_and_percentilePlot(delays, outputFolder, 'timeLag_providerAccountCreation1stTransaction', 'Time lag (days)', 45, 450, 50);

clear delays idxes;


%%%%%%%%%%% Duration between 1st car posted and 1st provider transaction (for providers who posted their 1st car and had their 1st transac in the window) %%%%%%%%%%%
idxes = ~isnan(provider1stTransacDate) & ~isnan(providerCreationDate) & provider1stTransacDate > providerCreationDate & ...
	providerCreationDate > tt_bounds(1) & providerCreationDate < tt_bounds(2) & ...
	provider1stTransacDate > tt_bounds(1) & provider1stTransacDate < tt_bounds(2);

delays = provider1stTransacDate(idxes) - providerCreationDate(idxes);

length(delays)

% Look at distribution
summary_func(delays)

makeHistogram_and_percentilePlot(delays, outputFolder, 'timeLag_providerCarCreation1stTransaction', 'Time lag (days)', 45, 450, 50);

clear delays idxes;

%%%%%%%%%%% Duration between account creation and 1st consumer transaction (for consumers who created account and had their 1st transac in period) %%%%%%%%%%%
idxes = ~isnan(consumer1stTransacDate) & consumer1stTransacDate > accountCreationDate & ...
		accountCreationDate > tt_bounds(1) & accountCreationDate < tt_bounds(2) & ...
		consumer1stTransacDate > tt_bounds(1) & consumer1stTransacDate < tt_bounds(2);

delays = consumer1stTransacDate(idxes) - accountCreationDate(idxes);


% Look at distribution
disp('===== Delay between consumer account creation and consumer 1st transaction: =====');
summary_func(delays)

disp('Median delay between account creation and first transaction for consumers:');
median(delays)

disp('Percentage of consumers whose first transaction occurs less than 20 days after account creation:');
mean(delays<20)

makeHistogram_and_percentilePlot(delays, outputFolder, 'timeLag_consumer1stTransaction', 'Time lag (days)', 45, 450, 50);

clear delays idxes;
